package org.micropayment.provider.oauth.mvc;

import java.util.Enumeration;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.security.oauth.provider.token.OAuthProviderTokenServices;
import org.springframework.security.oauth.provider.token.OAuthProviderToken;
import org.springframework.security.oauth.provider.ConsumerDetailsService;
import org.springframework.security.oauth.provider.ConsumerDetails;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.TreeMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth.provider.UnauthenticatedRequestTokenProcessingFilter;

/**
 * Controller for retrieving the model for and displaying the confirmation page
 * for access to a protected resource.
 *
 * @author jj
 *
 */
public class AccessConfirmationController extends AbstractController {

    private static Logger logger =
        LoggerFactory.getLogger(AccessConfirmationController.class);

    OAuthProviderTokenServices tokenServices;
    ConsumerDetailsService consumerDetailsService;

    protected ModelAndView handleRequestInternal(
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        String token = request.getParameter("oauth_token");
        if (token == null) {
            throw new IllegalArgumentException(
                    "A request token to authorize must be provided.");
        }
        OAuthProviderToken providerToken = getTokenServices().getToken(token);
        ConsumerDetails consumer = 
                getConsumerDetailsService().loadConsumerByConsumerKey(
                providerToken.getConsumerKey());
        String transaction_amount = request.getParameter("oauth_transaction_amount");
        logger.debug("parameter - oauth_transaction_amount: " + transaction_amount);
        String transaction_description = request.getParameter("oauth_transaction_description");
        String callback = request.getParameter("oauth_callback");
        TreeMap<String, Object> model = new TreeMap<String, Object>();
        model.put("oauth_token", token);
        if (callback != null) {
            model.put("oauth_callback", callback);
        }
        model.put("consumer", consumer);
        if (transaction_amount != null) {
            model.put("transaction_amount", transaction_amount);
        }
        if (transaction_description != null) {
            model.put("transaction_description", transaction_description);
        }
        return new ModelAndView("access_confirmation", model);
    }

    public OAuthProviderTokenServices getTokenServices() {
        return tokenServices;
    }

    public void setTokenServices(OAuthProviderTokenServices tokenServices) {
        this.tokenServices = tokenServices;
    }

    public ConsumerDetailsService getConsumerDetailsService() {
        return consumerDetailsService;
    }

    public void setConsumerDetailsService(
            ConsumerDetailsService consumerDetailsService) {
        this.consumerDetailsService = consumerDetailsService;
    }

}
